package com.znz.service;

import com.znz.domain.*;
import com.znz.util.common.Page;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: duyuxin
 * Date: 15-4-27
 * Time: 下午10:06
 * To change this template use File | Settings | File Templates.
 */
@Service
public class OrderService {
    @Autowired
    CommonService commonService;
    //TODO 获取所有的部位
    /**
     * @return   JSONArray
     */
    public List<Part> getPartAll(){
        List<Part> partList=Part.findAllParts();
        if(null!=partList){
            return  partList;
        }
        return null;
    };
    //TODO 根据部位获取该部位所包含的项目接口
    /**
     * @param partId 部位ID
     * @return JSONArray
     */
    public List<Project> getProjectByPartId(Long partId){
        List<Project> projectList=new ArrayList<Project>();
        if(null!=partId){
            projectList=commonService.getProjectListByPart(partId);
        }
        return   projectList;
    }
    //TODO 保存订单
    public JSONObject saveOrder(){
        JSONObject jsonObject=new JSONObject();
        return   jsonObject;
    }


    //TODO  根据省份代码或者根据市区代码获取其下所有的医院  订单派发页面
    /**
     * @param cityId       城市ID
     * @param province     省份ID
     * @param hospitalName 医院名称
     * @return
     */
    public Page<Thospital> getHospitalAllByCityId(Long cityId,Long province,String hospitalName,Page<Thospital> page){
        StringBuilder where=new StringBuilder();
        Map<String,Object> params=new HashMap<String, Object>();
        if(null!=province){
            where.append(" and o.province=:province ");
            params.put("province",province);
            if(null!=cityId){
                where.append(" and o.city=:cityId ");
                 params.put("cityId",cityId);
            }
        }
        if(null!=hospitalName){
             where.append(" and o.hospital_name like :hospitalName");
             params.put("hospitalName","%"+hospitalName+"%");
        }
        try {
            page.setTotal((int)Thospital.countThospitals());
            List<Thospital> thospitalList=Thospital.getHospitalAllByCityId(where.toString(),params,page.getOffset(),page.getOffset()+page.getLimit());
            page.setRows(thospitalList);
            return page;
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return page;
    }

    public List<Thospital> getHospitalAllByCityIdBytest(Long cityId,Long province,String hospitalName){
        StringBuilder where=new StringBuilder();
        Map<String,Object> params=new HashMap<String, Object>();
        if(null!=province){
            where.append(" and o.province=:province ");
            params.put("province",province);
            if(null!=cityId){
                where.append(" and o.city=:cityId ");
                params.put("cityId",cityId);
            }
        }
        if(null!=hospitalName){
            where.append(" and o.hospital_name like :hospitalName");
            params.put("hospitalName","%"+hospitalName+"%");
        }
        try {
            List<Thospital> thospitalList=Thospital.getHospitalAllByCityIdBytest(where.toString(),params);
            return thospitalList;
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    }
    //TODO 根据医院名称对医院进行模糊查询接口
    /**
     * @param hospitalName 医院名称
     * @return
     */
    public List<Thospital> getHospitalByLikeName(String hospitalName,Page page){
        StringBuilder stringBuilder=new StringBuilder();
        List<Object> params=new ArrayList<Object>();
        List<Thospital> thospitalList=new ArrayList<Thospital>();
        if(null!=hospitalName){
            stringBuilder.append(" and o.hospitalName like ?");
            params.add(hospitalName);
        }
        try {
            thospitalList=Thospital.getHospitalByWhere(stringBuilder.toString(), params, page.getOffset(), page.getOffset() + page.getLimit());
            return thospitalList;
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return thospitalList;
    }

	/**
	 * 保存订单信息
	 * @param order
	 * @return
	 */
	public Torder addOrder(Torder order) {
		 JSONObject jsonObjectOrder = JSONObject.fromObject(order);
         order.setOrederNo(UUID.randomUUID()+"");
         order.persist();
		return order;
	}

    /**
     * 保存订单分配医院信息 (项目信息)
     * @return
     */
    public boolean saveHospitalProject(Torder torder){
         boolean flag=false;

        return  false;
    }

    /**
     * 客服查看订单列表方法
     * @param userName 用户名称
     * @param page
     * @return
     */
    public Page<Torder> getTorderAllByTcustomer(Long customerId,String userName,Page<Torder> page){
       StringBuilder where=new StringBuilder();
       List<Object> params=new ArrayList<Object>();
       if(null!=userName){
           where.append(" and userName=? ");
           params.add(userName);
       }
       if(null!=customerId){
           Tcustomer tcustomer=Tcustomer.findTcustomer(customerId);
           if(null!=tcustomer){
               where.append(" and c_user_id=?");
               params.add(tcustomer.getId());
           }
       }
        try {
            page.setTotal((int)Torder.countTorders());
            List<Torder> torderList=Torder.getTorderAllByWhere(where.toString(),params,page.getOffset(),page.getOffset()+page.getLimit());
            page.setRows(torderList);
            return page;
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
       return page;
    }
    public List<Torder> getTorderAllByTcustomerTest(Long customerId,String userName){
        StringBuilder where=new StringBuilder();
        List<Object> params=new ArrayList<Object>();
        if(null!=userName){
            where.append(" and userName=? ");
            params.add(userName);
        }
        if(null!=customerId){
            Tcustomer tcustomer=Tcustomer.findTcustomer(customerId);
            if(null!=tcustomer){
                where.append(" and c_user_id=?");
                params.add(tcustomer.getId());
            }
        }
        try {
            List<Torder> torderList=Torder.getTorderAllByWheretest(where.toString(),params);
            return torderList;
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    }


    /**
     * 医院查看订单列表方法
     * @param userName  用户名称
     * @param page
     * @return
     */
    public Page<Torder> getTorderAllByThospital(String userName,Page<Torder> page){
          StringBuilder where=new StringBuilder();
          List<Object> params=new ArrayList<Object>();
          if(null!=userName){
              where.append(" and o.userName like ? ");
              params.add(userName);
          }
        try{
            page.setTotal((int)Torder.countTorders());
            List<Torder> torderList=Torder.getTorderAllByHospital(where.toString(),params,page.getOffset(),page.getOffset()+page.getLimit());
            page.setRows(torderList);
            return page;
        }catch (Exception e){
             e.printStackTrace();
        }
        return page;
    }

    /**
     * 根据订单ID查询项目列表
     * @param orderId
     * @return
     */
    public List<TorderHospitalProject> getProjectListbyOrderId(Long orderId){
        //TODO 根据订单ID查询项目列表 (获取订单所分配的医院)
        List<TorderHospitalProject>  projectList=new ArrayList<TorderHospitalProject>();
        if(null!=orderId){
            projectList=Torder.getProjectListbyOrderId(orderId);
            return projectList;
        }
        return projectList;
    }
}
